package com.yxinsur.imc.web.controller.business;

import com.yxinsur.imc.business.domain.ImcComm;
import com.yxinsur.imc.business.form.CommForm;
import com.yxinsur.imc.business.form.ExamCommForm;
import com.yxinsur.imc.business.service.BasicService;
import com.yxinsur.imc.business.service.IImcCommService;
import com.yxinsur.imc.business.vo.CommVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.exception.BusinessException;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 佣金报主Controller
 *
 * @author imc
 * @date 2021-05-25
 */
@Api(value = "佣金计算接口服务",tags = {"佣金计算接口服务"})
@RestController
@RequestMapping("/business/comm")
public class ImcCommController extends BaseController {
    @Autowired
    private IImcCommService imcCommService;
    @Autowired
    private BasicService basicService;

    /**
     * 查询佣金报主列表
     */
    @ApiOperation("查询佣金报表列表")
    @GetMapping("/list")
    public TableDataInfo list(CommForm commForm) {
        startPage();
        List<CommVo> list = imcCommService.selectImcCommList(commForm);
        return getDataTable(list);
    }

    /**
     * 导出佣金报主列表
     */
    /*@Log(title = "佣金报主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ImcComm imcComm) {
        List<ImcComm> list = imcCommService.selectImcCommList(imcComm);
        ExcelUtil<ImcComm> util = new ExcelUtil<ImcComm>(ImcComm.class);
        return util.exportExcel(list, "comm");
    }*/

    /**
     * 获取佣金报主详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imcCommService.selectImcCommById(id));
    }

    /**
     * 新增佣金报主
     */
    @Log(title = "佣金报主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImcComm imcComm) {
        return toAjax(imcCommService.insertImcComm(imcComm));
    }

    /**
     * 修改佣金报主
     */
    @Log(title = "佣金报主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImcComm imcComm) {
        return toAjax(imcCommService.updateImcComm(imcComm));
    }

    /**
     * 删除佣金报主
     */
    @ApiOperation("删除佣金主表数据")
    @Log(title = "佣金报主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(imcCommService.deleteImcCommById(id));
    }

    @ApiOperation("险种明细计算")
    @Log(title = "险种明细计算", businessType = BusinessType.INSERT)
    @PostMapping("/calculate")
    public AjaxResult calculate(@RequestParam ("orgId") Long orgId, @RequestParam ("calculateDate") Date calculateDate,@RequestParam ("fundId") Long fundId){
        try {
            imcCommService.calculate(orgId,calculateDate,fundId);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return AjaxResult.error("计算失败，请检查"+e.getMessage());
        }
        return AjaxResult.success("计算成功");
    }

    @ApiOperation("佣金明细计算")
    @Log(title = "佣金明细计算", businessType = BusinessType.INSERT)
    @PostMapping("/calculateDetail")
    public AjaxResult calculateDetail(@RequestParam ("commId") Long commId,@RequestParam ("orgId") Long orgId){
        ImcComm imcComm = null;
        List<Long> brokerIds = null;
        try {
            imcComm = imcCommService.calculateDetail(commId, orgId);
            brokerIds = imcCommService.asnyCalculateDetail(imcComm);
            imcCommService.calculateBasic(commId, brokerIds, imcComm.getSettleMonth());
        } catch (Exception e) {
            e.printStackTrace();
            imcComm.setStatus("A");
            imcComm.setRemark("计算发生异常，请检查"+e.getMessage());
            imcCommService.updateImcComm(imcComm);
            throw new BusinessException("计算发生异常，请检查");
        }
        return AjaxResult.success("计算中,请稍后查看");
    }


    @ApiOperation("提交审核")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examComm/{commIds}")
    public AjaxResult commitExamComm(@PathVariable ("commIds") Long[] commIds){
        try {
            imcCommService.examComm(commIds);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("审核成功");
    }


    @ApiOperation("审核通过")
    @Log(title = "审核通过", businessType = BusinessType.UPDATE)
    @PostMapping("/examPassComm")
    public AjaxResult examPassComm(ExamCommForm examCommForm){
        try {
            imcCommService.examPassComm(examCommForm);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("审核通过");
    }

    @ApiOperation("反结审核")
    @Log(title = "反结审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examCancelComm")
    public AjaxResult examCancelComm(ExamCommForm examCommForm){
        try {
            imcCommService.examCancelComm(examCommForm);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("反结审核通过");
    }


    @ApiOperation("退回审核")
    @Log(title = "退回审核", businessType = BusinessType.UPDATE)
    @PostMapping("/returnComm")
    public AjaxResult returnComm(ExamCommForm examCommForm){
        try {
            imcCommService.returnComm(examCommForm);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("反结审核通过");
    }
}
